package com.xxy.crm.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xxy.crm.base.BaseService;
import com.xxy.crm.dao.CusDevPlanMapper;
import com.xxy.crm.dao.SaleChanceMapper;
import com.xxy.crm.query.CusDevPlanQuery;
import com.xxy.crm.query.SaleChanceQuery;
import com.xxy.crm.utils.AssertUtil;
import com.xxy.crm.vo.CusDevPlan;
import com.xxy.crm.vo.SaleChance;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class CusDevPlanService extends BaseService<CusDevPlan,Integer> {
    @Resource
    private CusDevPlanMapper cusDevPlanMapper;
    @Resource
    private SaleChanceMapper saleChanceMapper;

    /**
     * 分页查询实现
     *
     * @param cusDevPlanQuery
     * @return
     */
    public Map<String, Object> queryCusDevPlanByParams(CusDevPlanQuery cusDevPlanQuery) {
        Map<String, Object> map = new HashMap<>();
        //开启分页
        PageHelper.startPage(cusDevPlanQuery.getPage(), cusDevPlanQuery.getLimit());
        //得到分页对应对象
        PageInfo<CusDevPlan> pageInfo = new PageInfo<>(cusDevPlanMapper.selectByParams(cusDevPlanQuery));

        //设置map对象
        map.put("code", 0);
        map.put("msg", "success");
        map.put("count", pageInfo.getTotal());
        //设置分页好的列表
        map.put("data", pageInfo.getList());
        return map;
    }

    /**
     * 添加操作
     * 1.参数校验
     *    机会id 非空 记录必须存在
     *    计划项内容非空
     *    计划项时间非空
     * 2. 参数默认值
     *    is_valid  1
     *    createDate 系统时间
     *    updateDate  系统时间
     * 3.执行添加 判断结果
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void addCusDevPlan(CusDevPlan cusDevPlan){
        //参数校验
        checkCusDevPlanParams(cusDevPlan);
        cusDevPlan.setIsValid(1);
        cusDevPlan.setCreateDate(new Date());
        cusDevPlan.setUpdateDate(new Date());
        AssertUtil.isTrue(cusDevPlanMapper.insertSelective(cusDevPlan)!=1,"计划项记录添加失败");
    }


/*    *
     * 1.参数校验
     * 机会id 非空 记录必须存在
     * 计划项内容非空
     * 计划项时间非空*/

    private void checkCusDevPlanParams(CusDevPlan cusDevPlan) {
        AssertUtil.isTrue(null == saleChanceMapper.selectByPrimaryKey(cusDevPlan.getSaleChanceId()) ,"系统异常，请重试");
        AssertUtil.isTrue(StringUtils.isBlank(cusDevPlan.getPlanItem()),"计划项内容不能为空");
        AssertUtil.isTrue(null ==cusDevPlan.getPlanDate(),"计划项时间不能为空");
    }

    /**
     * 更新操作
     * 1.参数校验
     *    机会id 非空 记录必须存在
     *    计划项内容非空
     *    计划项时间非空
     * 2. 参数默认值
     *    is_valid  1
     *    createDate 系统时间
     *    updateDate  系统时间
     * 3.执行添加 判断结果
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateCusDevPlan(CusDevPlan cusDevPlan){
        //参数校验
        AssertUtil.isTrue(null ==cusDevPlan.getId() ||
                null == saleChanceMapper.selectByPrimaryKey(cusDevPlan.getId()),"待更新的记录不存在");
        checkCusDevPlanParams(cusDevPlan );
        cusDevPlan.setUpdateDate(new Date());
        AssertUtil.isTrue(cusDevPlanMapper.updateByPrimaryKeySelective(cusDevPlan)!=1,"计划项记录更新失败");
    }

    /**
     * 删除计划项
     * 1.判断id是否为空，切数据存在
     * 2.修改isValid属性
     * 3.执行更新操作
     * @param id
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteCusDevPlan(Integer id) {
        AssertUtil.isTrue(id ==null,"待删除的记录不存在");
        CusDevPlan cusDevPlan = cusDevPlanMapper.selectByPrimaryKey(id);
        //设置记录无效
        cusDevPlan.setIsValid(0);
        cusDevPlan.setUpdateDate(new Date());
        //执行更新操作
        AssertUtil.isTrue(cusDevPlanMapper.updateByPrimaryKeySelective(cusDevPlan) != 1,"数据更新失败");

    }
}
